package com.dlu.safe_construction.service.impl;

import com.dlu.safe_construction.common.ApiResult;
import com.dlu.safe_construction.common.CommonEnum;
import com.dlu.safe_construction.common.utils.Utils;
import com.dlu.safe_construction.dto.AreaInfoDTO;
import com.dlu.safe_construction.dto.AreaNumDTO;
import com.dlu.safe_construction.dto.SpaceInfoDTO;
import com.dlu.safe_construction.mapper.AreaMapper;
import com.dlu.safe_construction.mapper.CarMapper;
import com.dlu.safe_construction.mapper.OwnerMapper;
import com.dlu.safe_construction.mapper.SpaceMapper;
import com.dlu.safe_construction.model.Area;
import com.dlu.safe_construction.model.Car;
import com.dlu.safe_construction.model.Owner;
import com.dlu.safe_construction.model.Space;
import com.dlu.safe_construction.service.AreaService;
import com.dlu.safe_construction.service.SpaceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by  lijiaxing on 2022/3/31 20:12
 *
 * @author lijiaxing
 */
@Service
@Slf4j
public class AreaServiceImpl implements AreaService {

    @Resource
    private AreaMapper areaMapper;

    @Resource
    private SpaceMapper spaceMapper;

    @Resource
    private SpaceServiceImpl spaceService;

    @Resource
    private CarMapper carMapper;

    @Resource
    private OwnerMapper ownerMapper;

    @Override
    @Transactional
    public ApiResult<Void> add(Area area) {
        if (Utils.isEmpty(area)) {
            return ApiResult.fail(7000, "停车场信息不能为空");
        } else {
            if (!CommonEnum.LatterJudge.LATTER_A_Z.isLatter(area.getAreaId().charAt(0))) {
                log.error("区域错误，请重新填写。param={}", area);
                return ApiResult.fail(7001, "请填写区域范围A-Z.");
            } else if (Utils.isEmpty(area.getSpaceNum()) || area.getSpaceNum() <= 0) {
                log.error("区域错误，请重新填写。param={}", area);
                return ApiResult.fail(7002, "区域包含车位数不能为空.");
            } else {
                int ret = areaMapper.insert(area);
                if (ret <= 0) {
                    return ApiResult.fail(6004);
                } else {
                    List<Space> list = SpaceInfoDTO.spaceCreateList(area);
                    spaceService.batchAdd(list);
                    return ApiResult.success();
                }
            }
        }
    }

    @Override
    @Transactional
    public ApiResult<Void> deleteById(String areaId) {
        int ret_space = spaceMapper.batchDeleteByAreaId(areaId);
        if (ret_space <= 0) {
            log.error("can not delete space by areaId.param={}", areaId);
            return ApiResult.fail(7003, "车位删除失败，请重试");
        }
        int ret_area = areaMapper.deleteByPrimaryKey(areaId);
        if (ret_area <= 0) {
            log.error("can not delete area by areaId.param={}", areaId);
            return ApiResult.fail(7004, "停车场删除失败，请重试");
        }
        return ApiResult.success();
    }

    @Override
    public ApiResult<Void> edit(AreaInfoDTO areaInfoDTO) {
        return null;
    }

    @Override
    public ApiResult<List<Area>> list() {
        List<Area> list = areaMapper.selectAll();
        return ApiResult.success(list);
    }

    @Override
    public ApiResult<AreaInfoDTO> searchByAreaId(String areaId) {
        Area area = areaMapper.selectByPrimaryKey(areaId);
        List<Space> spaces = spaceMapper.selectByAreaId(areaId);
        AreaInfoDTO areaInfoDTO = AreaInfoDTO.fromArea(area, spaces);
        return ApiResult.success(areaInfoDTO);

    }

    @Override
    public ApiResult<AreaNumDTO> countAll() {
        AreaNumDTO areaNumDTO = new AreaNumDTO();
        areaNumDTO.setAllSpace(spaceMapper.countAllSpace());
        areaNumDTO.setResSpace(spaceMapper.countSpaceByStatus(CommonEnum.SpaceStatus.NO_PARK.status));
        areaNumDTO.setUseSpace(areaNumDTO.getAllSpace() - areaNumDTO.getResSpace());
        return ApiResult.success(areaNumDTO);
    }

    private List<SpaceInfoDTO> createFromSpaces(List<Space> spaces) {
        List<SpaceInfoDTO> spaceInfoDTOS = new ArrayList<>();
        spaces.stream().forEach(space -> {
            SpaceInfoDTO spaceInfoDTO = new SpaceInfoDTO();
            spaceInfoDTO.setSpaceId(space.getSpaceId());
            spaceInfoDTO.setAreaId(space.getAreaId());
            spaceInfoDTO.setCarId(space.getCarId());
            spaceInfoDTO.setSpaceStatus(space.getSpaceStatus());
            spaceInfoDTO.setSpaceTime(space.getSpaceTime());
            spaceInfoDTO.setId(space.getId());
            List<Car> cars = carMapper.selectByCarId(space.getCarId());
            spaceInfoDTO.setOwnerId(cars.get(0).getOwnerId());
            Owner owner = ownerMapper.selectByPrimaryKey(spaceInfoDTO.getOwnerId());
            spaceInfoDTO.setOwnerName(owner.getOwnerName());
            spaceInfoDTO.setOwnerPhone(owner.getOwnerPhone());
            spaceInfoDTOS.add(spaceInfoDTO);
        });
        return spaceInfoDTOS;
    }
}
